package com.devbobcorn.misaka.common;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.devbobcorn.misaka.Misaka;
import com.devbobcorn.misaka.blocks.CandleMisakaCakeBlock;
import com.devbobcorn.misaka.blocks.ModBlocks;
import com.devbobcorn.misaka.items.ModItemTab;

import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.Rarity;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.registries.IForgeRegistry;

@EventBusSubscriber(modid = Misaka.MODID, bus = EventBusSubscriber.Bus.MOD)
public final class CommonModEventSubscriber {
    private static final Logger LOGGER = LogManager.getLogger(Misaka.MODID + " Mod Event Subscriber");

    /**
     * This method will be called by Forge when it is time for the mod to register
     * its Items. This method will always be called after the Block registry method.
     */
    @SubscribeEvent
    public static void onRegisterItems(final RegistryEvent.Register<Item> event) {
        final IForgeRegistry<Item> registry = event.getRegistry();
        // Automatically register BlockItems for all our Blocks
        ModBlocks.BLOCKS.getEntries().stream().map(RegistryObject::get)
                // You can do extra filtering here if you don't want some blocks to have an
                // BlockItem automatically registered for them
                .filter(block -> {
                    return !(block instanceof CandleMisakaCakeBlock);
                })
                // Register the BlockItem for the block
                .forEach(block -> {
                    Item.Properties properties = new Item.Properties().tab(ModItemTab.MISAKA_GROUP);

                    if (block == ModBlocks.GEKOTA_PINK.get()) {
                        // 1/24 Probabilityのピンクゲコタ!!!
                        properties.rarity(Rarity.EPIC);
                    }

                    BlockItem blockItem = new BlockItem(block, properties);
                    // Set its Registry Name...
                    blockItem.setRegistryName(block.getRegistryName());
                    // And register it...
                    registry.register(blockItem);
                });
        LOGGER.info("BlockItems Registered.");
    }
}